package cn.qidisoft.edu.hzjt.service.zyc.impl;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.zyc.PartyActivityMapper;
import cn.qidisoft.edu.hzjt.model.xb.PartyResponsibilityAttEn;
import cn.qidisoft.edu.hzjt.model.zyc.PartyActivity;
import cn.qidisoft.edu.hzjt.model.zyc.PartyActivityComment;
import cn.qidisoft.edu.hzjt.model.zyc.PartyActivityMember;
import cn.qidisoft.edu.hzjt.service.zyc.IPartyActivityService;
import cn.qidisoft.edu.hzjt.utils.LogType;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author zhangyuancheng
 * @create 2018-11-16 下午4:47
 * @descripition 活动/会议名称实现类
 */
@Service
public class PartyActivityServiceImpl implements IPartyActivityService {

    protected static final LoggerAdapter LOGGER =
            LoggerAdapterFactory.getLogger(LogType.HZJT_LOGGER.val);

    @Autowired
    private PartyActivityMapper partyActivityMapper;

    @Override
    public PaginationBean<PartyActivity> queryPartyActivityPageList(Map<String, Object> paramtersMap, Page page) {
        try {
            int c = partyActivityMapper.queryPartyActivityPageCount(paramtersMap);
            int totalRecords = 0;
            totalRecords = (int) c;
            PaginationBean<PartyActivity> pageHolder =
                    new PaginationBean<PartyActivity>(page, totalRecords);
            List<PartyActivity> resultList =
                    partyActivityMapper.queryPartyActivityPageList(paramtersMap,
                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                                    pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("querySemesterSummaryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                    this.getClass().getName(), ToStringBuilder.reflectionToString(
                            paramtersMap + "===" + page, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
        }
    }

    @Override
    public void setPartyFixedDateActivityDay(Integer partyFixedDateActivityDay) {
        partyActivityMapper.setPartyFixedDateActivityDay(partyFixedDateActivityDay);
    }


    @Override
    public String queryPartyFixedDateActivityDay() {
        return partyActivityMapper.queryPartyFixedDateActivityDay();
    }


    @Override
    public void addPartyActivity(PartyActivity partyActivity) {

        //添加基本信息
        partyActivity.setgCJL("");
        partyActivityMapper.addPartyActivity(partyActivity);
        Integer parentId = partyActivity.gethDID();

         //添加参与人员
        List<PartyActivityMember> partyActivityMemberList = partyActivity.getPartyActivityMembers();
        for(int i=0;i<partyActivityMemberList.size();i++){
            PartyActivityMember partyActivityMember = partyActivityMemberList.get(i);
            partyActivityMember.setParentId(parentId);
            partyActivityMapper.addPartActivityMember(partyActivityMember);
        }

        //添加请假人员
        List<PartyActivityMember> partyActivityMemberQingjiaList = partyActivity.getPartyActivityMembersQingjia();
        for(int i=0;i<partyActivityMemberQingjiaList.size();i++){
            PartyActivityMember partyActivityMember = partyActivityMemberQingjiaList.get(i);
            partyActivityMember.setParentId(parentId);
            partyActivityMapper.addPartActivityMember(partyActivityMember);
        }

    }


    @Override
    public void updatePartyActivity(PartyActivity partyActivity) {
        //添加基本信息
        partyActivity.setgCJL("");
        partyActivityMapper.updatePartyActivity(partyActivity);
        Integer parentId = partyActivity.gethDID();

        //删除参与人员和请假人员
        partyActivityMapper.deletePartyActvityMembersByParentId(parentId);

        //添加参与人员
        List<PartyActivityMember> partyActivityMemberList = partyActivity.getPartyActivityMembers();
        for(int i=0;i<partyActivityMemberList.size();i++){
            PartyActivityMember partyActivityMember = partyActivityMemberList.get(i);
            partyActivityMember.setParentId(parentId);
            partyActivityMapper.addPartActivityMember(partyActivityMember);
        }

        //添加请假人员
        List<PartyActivityMember> partyActivityMemberQingjiaList = partyActivity.getPartyActivityMembersQingjia();
        for(int i=0;i<partyActivityMemberQingjiaList.size();i++){
            PartyActivityMember partyActivityMember = partyActivityMemberQingjiaList.get(i);
            partyActivityMember.setParentId(parentId);
            partyActivityMapper.addPartActivityMember(partyActivityMember);
        }

    }

    @Override
    public void updatePartyActivityObject(PartyActivity partyActivity) {
        partyActivityMapper.updatePartyActivity(partyActivity);
    }

    @Override
    public void delete(Integer[] ids) {
        if(ids!=null && ids.length>0){
            for(int i=0;i<ids.length;i++){
                partyActivityMapper.delete(ids[i]);
            }
        }
    }


    @Override
    public PartyActivity getPartyActivityById(Integer id) {
        return partyActivityMapper.getPartyActivityById(id);
    }

    @Override
    public void batchInsertsFiles(List<PartyResponsibilityAttEn> records) {
        partyActivityMapper.batchInsertsFiles(records);
    }


    @Override
    public List<PartyResponsibilityAttEn> queryFilesList(PartyResponsibilityAttEn entity) {
        return partyActivityMapper.queryFilesList(entity);
    }

    @Override
    public void addPartyActivityComment(PartyActivityComment partyActivityComment) {
        partyActivityMapper.addPartyActivityComment(partyActivityComment);
    }

    @Override
    public void batchInsertsCommentFiles(List<PartyResponsibilityAttEn> records) {
        partyActivityMapper.batchInsertsCommentFiles(records);
    }

    @Override
    public PaginationBean<PartyActivityComment> queryPartyActivityCommentsPageList(Map<String, Object> paramtersMap, Page page) {
        try {
            int c = partyActivityMapper.queryPartyActivityCommentsPageCount(paramtersMap);
            int totalRecords = 0;
            totalRecords = (int) c;
            PaginationBean<PartyActivityComment> pageHolder =
                    new PaginationBean<PartyActivityComment>(page, totalRecords);
            List<PartyActivityComment> resultList =
                    partyActivityMapper.queryPartyActivityCommentsPageList(paramtersMap,
                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                                    pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("querySemesterSummaryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                    this.getClass().getName(), ToStringBuilder.reflectionToString(
                            paramtersMap + "===" + page, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
        }
    }

    @Override
    public int queryPartyActivityCommentsPageCount(Map<String, Object> paramtersMap) {
        return partyActivityMapper.queryPartyActivityCommentsPageCount(paramtersMap);
    }

	@Override
	public List<PartyActivity> queryList(Map<String, Object> paramtersMap) {
		// TODO Auto-generated method stub
		return partyActivityMapper.queryList(paramtersMap);
	}
}
